package com.quincysx.messager;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.IBinder.DeathRecipient;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    private Messenger mRemoteMessenger;
    private Messenger mClientMessenger = new Messenger(new ClientHandler());

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bindService();
    }

    /**
     * 绑定 Service
     */
    private void bindService() {
        Intent intent = new Intent(this, RemoteService.class);
        bindService(intent, mServiceConnection, BIND_AUTO_CREATE);
    }

    public void onClick(View view) {
        String msg = "Hello 服务端你好";

        Message message = Message.obtain(null, RemoteContract.SEND_SERVICE_MSG);
        Bundle bundle = new Bundle();
        bundle.putString(RemoteContract.EXT_MSG, msg);
        message.setData(bundle);

        //设置客户端 Messenger
        message.replyTo = mClientMessenger;

        if (mRemoteMessenger != null) {
            try {
                mRemoteMessenger.send(message);
                Log.e("==客户端发送消息==", msg);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onDestroy() {
        if (mRemoteMessenger != null) {
            mRemoteMessenger.getBinder().unlinkToDeath(mDeathRecipient, 0);
        }
        unbindService(mServiceConnection);
        super.onDestroy();
    }

    /**
     * 客户端消息处理
     */
    private static class ClientHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case RemoteContract.SEND_CLIENT_MSG:
                    Bundle data = msg.getData();
                    Log.e("==客户端收到消息==", data.getString(RemoteContract.EXT_MSG));
                    break;
                default:
            }
        }
    }

    /**
     * Service 连接器
     */
    private ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mRemoteMessenger = new Messenger(service);
            try {
                mRemoteMessenger.getBinder().linkToDeath(mDeathRecipient, 0);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mRemoteMessenger = null;
        }
    };

    /**
     * 连接异常监听
     */
    private DeathRecipient mDeathRecipient = new DeathRecipient() {
        @Override
        public void binderDied() {
            mRemoteMessenger = null;
            bindService();
        }
    };
}
